import http from '../utils/http'
import constant from '../utils/constant'
import QRCode from 'qrcodejs2'

export default {
  name: 'profile',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      orders: [],
      orderStatus: 'PAID',
      agent: {},
      wechatAccount: {},
      exchangeRates: {},
      showLoadingText: true,
    }
  },
  created() {
    this.fetchOderList()
  },
  methods: {
    changeOrderStatus(orderStatus) {
      this.orderStatus = orderStatus
    },
    fetchOderList() {
      let message = this.$message.loading({
        message: '正在加载数据...',
        align: 'center'
      })
      let mobile = this.$store.getters.getMobile
      http.get(constant.orders, {mobile: mobile, status: this.orderStatus}, data => {
        this.orders = data.orders
        this.agent = data.agent
        this.wechatAccount = data.wechatAccount
        this.exchangeRates = data.exchangeRates
        message.close()
      }, data => {
        message.close(data.message)
      })
    },

    showModal(id) {
      let that = this    
      this.$refs['invoice-modal'].show()
      
      this.$root.$on('bv::modal::shown', (bvEvent, modalId) => {
       
        document.getElementById("qrcode").innerHTML=''
        let qrcode = new QRCode('qrcode', {
          width: 132,  
          height: 132,
          colorDark : "#000",
          colorLight : "#fff",
          correctLevel: QRCode.CorrectLevel.L
        })

        http.post(constant.invoice, {orderId: id}, data => {
          //开票二维码
          setTimeout(function() {
            that.showLoadingText = false
            qrcode.clear(); // 清除代码
            qrcode.makeCode(data.invoiceQrcode); // 生成另外一个二维码
            that.$message.closeAll()
          },1000)
         
        }, data => {
          qrcode.clear(); // 清除代码
          that.$message.error({
            message: data.message,
            align: 'center',
            showClose: false,
            duration: 3000
          })
        })
      })
      this.$root.$on('bv::modal::hidden', (bvEvent, modalId) => { 
        that.showLoadingText = true   
      })
    }
  },
  watch: {
    'orderStatus' (to, from) {
      this.fetchOderList()
    },
    '$route' (to, from) {
      //this.$router.go(0)
      if (to.params.refresh) {
        this.fetchOderList()
      }
    }
  }
}
